# Using CPP compiler to compile c code
CC=g++
OBJS=tf.o tagfolder.o string_list.o tf_list.o tf_list_test.o tagfs.o tf_log.o tf_mem.o tf_log_test.o
TFLIB_OBJS=tagfolder.o string_list.o tf_list.o tf_log.o tf_mem.o
TESTS=tf_list_test tf_log_test
TARGET=tf tfmount $(TESTS)
MOUNT_POINT=/tmp/tf

# Detect debug config
ifdef DEBUG
	CFLAGS:= -DDEBUG -D_FILE_OFFSET_BITS=64 -g
	SYSLIBS:=-lsqlite3 -lfuse
else
	# add debug symbols for develop purpose
	CFLAGS:=-D_FILE_OFFSET_BITS=64 -g
	SYSLIBS:=-lsqlite3 -lfuse
endif

# All the targets
all:$(TARGET)


tf: $(TFLIB_OBJS) tf.o
	$(CC) $(CFLAGS)  $(TFLIB_OBJS) tf.o $(SYSLIBS) -o $@

tfmount:$(TAGFS_OBJS) tagfs.o
	$(CC) $(CFLAGS) $(TFLIB_OBJS) tagfs.o $(SYSLIBS) -o $@

tf_list_test: $(TFLIB_OBJS) tf_list_test.o
	$(CC) $(CFLAGS) $(TFLIB_OBJS) tf_list_test.o $(SYSLIBS) -o $@

tf_log_test: $(TFLIB_OBJS) tf_log_test.o
	$(CC) $(CFLAGS) $(TFLIB_OBJS) tf_log_test.o $(SYSLIBS) -o $@

%.o:%.c
	$(CC) $(CFLAGS) -c $< -o $@

.PHONY:test rebuild test_mount

# Run unit tests
test: $(TESTS)
	-@echo "############### Testing list functions #############"
	-./tf_list_test
	-@echo "############### Testing log functions #############"
	./tf_log_test

rebuild: clean all

# Running the file operation test
test_mount:tfmount
	-mkdir $(MOUNT_POINT)
	-@echo "############### Testing mount #############"
	./tfmount $(MOUNT_POINT)
	-@echo "############### Testing tag listing #############"
	-ls $(MOUNT_POINT)
	-@echo "############### Testing tag creation #############"
	-mkdir $(MOUNT_POINT)/hello_test
	-@echo "############### Testing tag deleting #############"
	-rmdir $(MOUNT_POINT)/hello_test
	-@echo "############### Testing unmount #############"
	fusermount -u $(MOUNT_POINT)
	-rmdir $(MOUNT_POINT)

clean:
	rm $(TARGET)
	rm $(OBJS)
